package cn.edu.scau.cmi.liangzijian.singleton;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.control.TitledPane;
import javafx.scene.input.MouseEvent;


public class UIController {

    @FXML
    private ResourceBundle resources;

    @FXML
    private URL location;

    @FXML
    private Button createMultition;

    @FXML
    private Button createSingleton;

    @FXML
    private TextField nameMultition;

    @FXML
    private TextField nameSingleton;

    @FXML
    private TextArea showFieldMultition;

    @FXML
    private TextArea showFieldSingleton;

    @FXML
    private Label tipMultition;

    @FXML
    private Label tipSingleton;

    @FXML
    private TitledPane x1;

    private boolean hasSingleton = false;
    
    @FXML
    void createMultitionObject(MouseEvent event) {
    	String tmpName = nameMultition.getText().trim();
    	if(tmpName == null || 
    			tmpName.equals("")) {
    		tipMultition.setText("名称不能为空");
    	} else {
    		Marshal marshal = Marshal.getInstance(tmpName);
    		if(marshal != null) {
    			if(!marshal.isNew()) {
        			tipMultition.setText("该对象已存在");
    			} else {
        			tipMultition.setText("创建成功");
    			}

    			showFieldMultition.appendText(marshal.getName()+marshal.toString()+"\n");
    			
    		} else {
    			tipMultition.setText("可建对象数目已达最大值");
    		}
    	}
    }

    @FXML
    void createSingletonObject(MouseEvent event) {
    	String tmpName = nameSingleton.getText().trim();
    	if(tmpName == null || 
    			tmpName.equals("")) {
    		tipSingleton.setText("名称不能为空");
    	} else {
    		Chairman chairman = Chairman.getInstance(tmpName);
    		if(chairman != null) {
    			if(hasSingleton) {
    				tipSingleton.setText("可建对象数目已达最大值");
    			} else {
    				tipSingleton.setText("创建成功");
    			}
    			showFieldSingleton.appendText(chairman.getName()+chairman.toString()+"\n");
    			
    			hasSingleton = true;
    		}
    	}
    }

    @FXML
    void initialize() {
        assert createMultition != null : "fx:id=\"createMultition\" was not injected: check your FXML file 'UI.fxml'.";
        assert createSingleton != null : "fx:id=\"createSingleton\" was not injected: check your FXML file 'UI.fxml'.";
        assert nameMultition != null : "fx:id=\"nameMultition\" was not injected: check your FXML file 'UI.fxml'.";
        assert nameSingleton != null : "fx:id=\"nameSingleton\" was not injected: check your FXML file 'UI.fxml'.";
        assert showFieldMultition != null : "fx:id=\"showFieldMultition\" was not injected: check your FXML file 'UI.fxml'.";
        assert showFieldSingleton != null : "fx:id=\"showFieldSingleton\" was not injected: check your FXML file 'UI.fxml'.";
        assert tipMultition != null : "fx:id=\"tipMultition\" was not injected: check your FXML file 'UI.fxml'.";
        assert tipSingleton != null : "fx:id=\"tipSingleton\" was not injected: check your FXML file 'UI.fxml'.";
        assert x1 != null : "fx:id=\"x1\" was not injected: check your FXML file 'UI.fxml'.";


    }

}
